﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SponsorshipManager.Database;

namespace SponsorshipManager.BusinessLayer
{
    public class NoteDTO : IDbBasedObject
    {
        public string Title{get;set;}
        public int NoteId { get; set; }
        public bool NeedsToSave { get; set; }
        public string Content {get;set;}
        public int ReferenceId {get; set;}
        public string NoteType {get; set;}


        internal static List<NoteDTO> Notes (string NoteType, int ReferenceId){
            List<NoteDTO> ret = new List<NoteDTO>();

            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.AddParameter("ReferenceId", DbType.Int32, ReferenceId);
            sp.AddParameter("NoteType", DbType.String, NoteType);
            sp.Name = "uspGetNotes";
            sp.Token = User.CurrentUser.Token;
            int result = sp.Execute();
            if(result > 0)
            {
                foreach(DataRow dr in sp.QueryResult.Rows)
                {
                    NoteDTO n = new NoteDTO();
                    n.LoadDataRow(dr);
                    ret.Add(n);
                }
            }
            return ret;
        }

        public bool Load()
        {
            StoredProcedure sp = DatabaseAdapter.Database.StoredProcedure();
            sp.AddParameter("NoteId", DbType.Int32, NoteId);
            sp.Name = "uspGetNote";
            sp.Token = User.CurrentUser.Token;
            int ret = sp.Execute();
            if (ret == 1) LoadDataRow(sp.QueryResult.Rows[0]);
                
            return ret == 1 ;
        }
        
        private bool LoadDataRow(DataRow dr) 
        {
            Title = dr["Title"].ToString();
            Content = dr["Content"].ToString();
            ReferenceId = ReferenceId;
            NoteType = NoteType;
            NoteId = (int)dr["NoteId"];
            return true;
        
        }

        public bool Save()
        {
            bool ret = false;
            if (NeedsToSave) 
            {
                throw new NotImplementedException();
            }
            return ret;
        }
    }
}
